package com.cn.kabalifeuser.view;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by 晓林 on 2020/3/31.
 * function:
 */
public class CustEdInput implements TextWatcher {

    public static int MAX_COUNT = 150;
    public static boolean ignoreCnOrEn = true;
    public static EditText etSummary;
    public static TextView tvCountNum;
    private int editStart;
    //
    private int editEnd;

    public CustEdInput(int MAX_COUNT, EditText etSummary, TextView tvCountNum) {
        this.MAX_COUNT = MAX_COUNT;
        this.etSummary = etSummary;
        this.tvCountNum = tvCountNum;
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        editStart = etSummary.getSelectionStart();
        editEnd = etSummary.getSelectionEnd();

        // 先去掉监听器，否则会出现栈溢出
        etSummary.removeTextChangedListener(this);

        if (ignoreCnOrEn) {
            //当输入字符个数超过限制的大小时，进行截断操作
            while (this.calculateLengthIgnoreCnOrEn(editable.toString()) > MAX_COUNT) {
                editable.delete(editStart - 1, editEnd);
                editStart--;
                editEnd--;
            }
        } else {
            // 因为是中英文混合，单个字符而言，calculateLength函数都会返回1
            while (calculateLength(editable.toString()) > MAX_COUNT) { // 当输入字符个数超过限制的大小时，进行截断操作
                editable.delete(editStart - 1, editEnd);
                editStart--;
                editEnd--;
            }
        }

        etSummary.setSelection(editStart);

        // 恢复监听器
        etSummary.addTextChangedListener(this);

        //update
        configCount();
    }

    //    public CustEdInput(int MAX_COUNT, EditText etSummary, TextView tvCountNum) {
//        this.MAX_COUNT = MAX_COUNT;
//        this.etSummary = etSummary;
//        this.tvCountNum = tvCountNum;
//    }
//    int MAX_COUNT = 150;
//    boolean ignoreCnOrEn = true;
//    EditText etSummary;
//    TextView tvCountNum;
//
//    private TextWatcher mTextWatcher = new TextWatcher() {
//
//        private int editStart;
//
//        private int editEnd;
//
//        @Override
//        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//        }
//
//        @Override
//        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//        }
//
//        @Override
//        public void afterTextChanged(Editable editable) {
//
//            editStart = etSummary.getSelectionStart();
//            editEnd = etSummary.getSelectionEnd();
//
//            // 先去掉监听器，否则会出现栈溢出
//            etSummary.removeTextChangedListener(mTextWatcher);
//
//            if (ignoreCnOrEn) {
//                //当输入字符个数超过限制的大小时，进行截断操作
//                while (calculateLengthIgnoreCnOrEn(editable.toString()) > MAX_COUNT) {
//                    editable.delete(editStart - 1, editEnd);
//                    editStart--;
//                    editEnd--;
//                }
//            } else {
//                // 因为是中英文混合，单个字符而言，calculateLength函数都会返回1
//                while (calculateLength(editable.toString()) > MAX_COUNT) { // 当输入字符个数超过限制的大小时，进行截断操作
//                    editable.delete(editStart - 1, editEnd);
//                    editStart--;
//                    editEnd--;
//                }
//            }
//
//            etSummary.setSelection(editStart);
//
//            // 恢复监听器
//            etSummary.addTextChangedListener(mTextWatcher);
//
//            //update
//            configCount();
//        }
//    };
//
//
    private static int calculateLengthIgnoreCnOrEn(CharSequence c) {
        int len = 0;
        for (int i = 0; i < c.length(); i++) {
            len++;
        }
        return len;
    }

    //
    public static void configCount() {
        if (ignoreCnOrEn) {
            int nowCount = calculateLengthIgnoreCnOrEn(etSummary.getText().toString());
            //
            tvCountNum.setText(String.valueOf((MAX_COUNT - nowCount)) + "/" + MAX_COUNT);
        } else {
            long nowCount = calculateLength(etSummary.getText().toString());
            //
            tvCountNum.setText(String.valueOf((MAX_COUNT - nowCount)) + "/" + MAX_COUNT);
        }

    }

    //
    private static long calculateLength(CharSequence c) {
        double len = 0;
        for (int i = 0; i < c.length(); i++) {
            int tmp = (int) c.charAt(i);
            if (tmp > 0 && tmp < 127) {
                len += 0.5;
            } else {
                len++;
            }
        }
        return Math.round(len);
    }
}
